from re import compile, sub
from time import strftime, localtime
from pprint import pprint as pp

from pandas import Timestamp
from requests import get
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from requests.packages.urllib3 import disable_warnings
from pymysql import connect

disable_warnings(InsecureRequestWarning)

first_regex = compile(".*(https://www.iesdouyin.com/share/video/.*/?region=CN&mid=.*?)&")
second_regex = compile(".*/(.*?)/")


class DouYinSpider:

    def __init__(self):
        self.video_list = []
        self.connect = connect(host="120.77.183.17", user="kxx", password="$5#g#hWS@Es7mexj", charset="utf8")

    @staticmethod
    def dual_timestamp(timestamp_: str) -> str:
        utc_date = str(Timestamp(timestamp_, unit='s', tz='Asia/Shanghai'))
        return utc_date.split("+")[0]

    def get_user_msg(self, data: str):
        link_list = list(set(first_regex.findall(data)))

        # 获取数据
        for link in link_list:
            aweme_id = second_regex.search(link).group(1)

            resp = get("https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={}".format(aweme_id),
                       verify=False).json()

            try:
                video = {
                    "nickname": sub("[^\\w]", " ", resp['item_list'][0]['author']['nickname']),  # 短视频主人名称
                    "short_id": resp['item_list'][0]['author']['short_id'],  # 短视频 短id
                    "signature": sub("[^\\w]", " ", resp['item_list'][0]['author']['signature']),  # 个人介绍
                    "uid": resp['item_list'][0]['author']['uid'],  # 短视频主人 主键
                    "aweme_id": resp['item_list'][0]['aweme_id'],  # 短视频id
                    "create_time": self.dual_timestamp(resp['item_list'][0]['create_time']),  # 创建时间
                    "desc": sub("[^\\w]", " ", resp['item_list'][0]['desc']),  # 短视频描述
                    "duration": resp['item_list'][0]['duration'],  # 短视频时长
                    "comment_count": resp['item_list'][0]['statistics']['comment_count'],  # 评论数量
                    "digg_count": resp['item_list'][0]['statistics']['digg_count'],  # 点赞数量
                    "video_url": link  # 短视频url
                }
            except IndexError as e:
                print(e)
                pp(resp)
                print(link)
                continue

            self.video_list.append(video)

    def write_to_sql(self):

        sql = """
        INSERT IGNORE INTO alibb.`alibb_dy` (
          nick_name,
          short_id,
          signature,
          uid,
          aweme_id,
          create_time,
          video_desc,
          duration,
          comment_count,
          digg_count,
          video_url
        )
        VALUES 
        """

        length = len(self.video_list)

        for num, value in enumerate(self.video_list):
            sql += "("
            sql += f"'{value['nickname']}',"
            sql += f"'{value['short_id']}',"
            sql += f"'{value['signature']}',"
            sql += f"'{value['uid']}',"
            sql += f"'{value['aweme_id']}',"
            sql += f"'{value['create_time']}',"
            sql += "'{}',".format(value['desc'].replace("\n", " "))
            sql += f"'{value['duration']}',"
            sql += f"'{value['comment_count']}',"
            sql += f"'{value['digg_count']}',"
            sql += f"'{value['video_url']}'"
            sql += ")" if length - 1 == num else "),"

        with self.connect.cursor() as cursor:
            try:
                cursor.execute(sql)
                self.connect.commit()
            except Exception as e:
                self.connect.rollback()
                print(e)
                print(sql)

        self.connect.close()


def response(flow):
    if "https://api3-core-c-hl.amemv.com/aweme/v2/feed" in flow.request.url:
        print("1111111\n" * 5)
        resp = flow.response

        data = resp.content.decode(encoding='utf8', errors='ignore')

        spider = DouYinSpider()

        # 获取数据
        spider.get_user_msg(data)

        # 保存数据
        spider.write_to_sql()

        print(f'成功, {strftime("%Y-%m-%d %H:%M:%S", localtime())}')
